﻿/*
 * KLF Headless Browser
 * Written by Kris Oye <kristianoye@gmail.com>
 * Copyright (C) 2011-2012.  All rights reserved.
 */
namespace KLF.Lib.Browser.DOM.HTML
{
    #region Using Statements

    using System;
    using System.Linq;

    #endregion

    public class HTMLTableSectionElement : HTMLBlockBaseElement
    {
        #region Constructors

        internal HTMLTableSectionElement(Document owner, string tagName)
            : base(owner, tagName)
        {
        }

        #endregion

        #region DOM Level 2 HTML Compliance


        /// <summary>
        /// Alignment character for cells in a column. 
        /// 
        /// See the char attribute definition in HTML 4.01.
        /// </summary>
        public string ch
        {
            get
            {
                return getAttribute(HTMLConstants.HTML_ATT_CHAR);
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                    removeAttribute(HTMLConstants.HTML_ATT_CHAR);
                else
                    setAttribute(HTMLConstants.HTML_ATT_CHAR, value);
            }
        }

        /// <summary>
        /// Offset of alignment character. 
        /// 
        /// See the charoff attribute definition in HTML 4.01.
        /// </summary>
        public string chOff
        {
            get
            {
                return getAttribute(HTMLConstants.HTML_ATT_CHAROFF);
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                    removeAttribute(HTMLConstants.HTML_ATT_CHAROFF);
                else
                    setAttribute(HTMLConstants.HTML_ATT_CHAROFF, value);
            }
        }

        /// <summary>
        /// Delete a row from this section. 
        /// </summary>
        /// 
        /// <param name="index">The index of the row to be deleted, or -1 to 
        /// delete the last row. This index starts from 0 and is relative only
        /// to the rows contained inside this section, not all the rows in 
        /// the table.</param>
        public void deleteRow(int index)
        {
            HTMLCollection r = rows;

            if (index < -1 || index > r.length)
                throw new DOMException(DOMException.INDEX_SIZE_ERR);
            else
            {
                Node n = r.item(((index == -1 || index == r.length) ? index - 1 : index));
                n.Parent.removeChild(n);
            }
        }

        /// <summary>
        /// Insert a row into this section. The new row is inserted immediately
        /// before the current indexth row in this section. If index is -1 or 
        /// equal to the number of rows in this section, the new row is 
        /// appended. 
        /// </summary>
        /// 
        /// <param name="index">The row number where to insert a new row. This 
        /// index starts from 0 and is relative only to the rows contained 
        /// inside this section, not all the rows in the table.</param>
        /// 
        /// <returns>The newly created row.</returns>
        public HTMLElement insertRow(int index)
        {
            HTMLCollection r = rows;
            HTMLTableRowElement row = (HTMLTableRowElement)Owner.createElement(HTMLConstants.HTML_TAG_TABLEROW);

            if (index < -1 || index > r.length)
            {
                throw new DOMException(DOMException.INDEX_SIZE_ERR);
            }
            else if (index == -1 || index == r.length)
            {
                appendChild(row);
            }
            else
            {
                r.item(index)
                    .Parent
                    .insertBefore(row, r.item(index));
            }
            return row;
        }

        /// <summary>
        /// The collection of rows in this table section.
        /// </summary>
        public HTMLCollection rows
        {
            get
            {
                Type rowType = typeof(HTMLTableRowElement);
                return new HTMLCollection(Owner, Elements
                    .Where(e => e.GetType() == rowType)
                    .Cast<Node>());
            }
        }

        /// <summary>
        /// Vertical alignment of cell data in column. 
        /// 
        /// See the valign attribute definition in HTML 4.01.
        /// </summary>
        public string vAlign
        {
            get
            {
                return getAttribute(HTMLConstants.HTML_ATT_VALIGN);
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                    removeAttribute(HTMLConstants.HTML_ATT_VALIGN);
                else
                    setAttribute(HTMLConstants.HTML_ATT_VALIGN, value);
            }
        }

        #endregion

        #region Custom Attributes

        /// <summary>
        /// Returns the containing table element or null if unattached.
        /// </summary>
        public HTMLTableElement tableElement
        {
            get
            {
                HTMLElement p = this;

                do
                {
                    p = p.Parent as HTMLElement;
                }
                while ((p != null) && !(p is HTMLTableElement));

                return (p != null ? p as HTMLTableElement : null);

            }
        }

        #endregion
    }
}
